<?php
require $_SERVER['DOCUMENT_ROOT'].'/../_core/common.php';

$msg = array();

//权限验证
if( Role::init() == false ){
	$msg['error'] = false;
	$msg['string'] = '权限不足！';
	$myjson = json_encode($msg); 
	echo $myjson; 
	exit;
}

$db = new DB('news');

$id = isset($_POST['id']) ? h($_POST['id']) : '';

$news_sql = 'select title_id,title,category_id,create_uid,static_url,content from news where title_id="'.$id.'" and is_del<3';
$news_rs = $db->query($news_sql)->getOne();

if( $news_rs['title_id'] == '' ){//id不存在
	$msg['error'] = false;
	$msg['string'] = '参数有误！';
}else{
	//更新is_del字段
	$data=array('is_del'=>1,'edit_uid'=>Sessions::get('uid'),'updated'=>time());
	$db->table('news')->where('title_id="'.$id.'"')->update($data)->run();
	
	//更新counts字段
	$category_sql = 'select counts from categories where id='.$news_rs['category_id'];
	$category_rs = $db->query($category_sql)->getOne();
	$data=array('counts'=>($category_rs['counts']+1));
	$db->table('categories')->where('id='.$news_rs['category_id'])->update($data)->run();
	
	//重命名静态文件
	/*$static_path = $root.'www'.substr($news_rs['static_url'], 20);
	$news_content_array = explode('_ueditor_page_break_tag_', $news_rs['content']);
	$pages = count($news_content_array);
	for( $page=0; $page<=$pages; $page++ ){
		if( $page == 0 ){
			$path = $static_path.'/';
		}else{
			$path = $static_path.'-'.$page.'/';
		}
		
		$old_php_filename = $path.'index_del.php';
		$new_php_filename = $path.'index.php';
		if( file_exists( $old_php_filename) ){
			@rename($old_php_filename, $new_php_filename);
		}
		
		$old_html_filename = $path.'index_del.html';
		$new_html_filename = $path.'index.html';
		if( file_exists( $old_html_filename) ){
			@rename($old_html_filename, $new_html_filename);
		}
	}*/
	
	//记录动作日志
	Log::logs_articles($news_rs['category_id'], 5, $news_rs['title_id'], $news_rs['title']);

	$msg['error'] = true;
	$msg['string'] = '成功恢复新闻['.$news_rs['title_id'].']《'.$news_rs['title'].'》!';
}
$myjson = json_encode($msg); 
echo $myjson; 
exit;